package com.cloud.mall.pms.service.impl;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.pms.dao.PmsProductAttributeCategoryDao;
import com.cloud.mall.pms.dto.PmsBrandParam;
import com.cloud.mall.pms.dto.PmsProductAttributeCategoryItem;
import com.cloud.mall.pms.mapper.PmsBrandMapper;
import com.cloud.mall.pms.mapper.PmsProductAttributeCategoryMapper;
import com.cloud.mall.pms.mapper.PmsProductMapper;
import com.cloud.mall.pms.model.*;
import com.cloud.mall.pms.out.PmsBrandOut;
import com.cloud.mall.pms.out.PmsProductAttributeCategoryOut;
import com.cloud.mall.pms.service.PmsBrandService;
import com.cloud.mall.pms.service.PmsProductAttributeCategoryService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PmsProductAttributeCategoryServiceImpl implements PmsProductAttributeCategoryService {
    @Autowired
    private PmsProductAttributeCategoryMapper productAttributeCategoryMapper;
    @Autowired
    private PmsProductAttributeCategoryDao productAttributeCategoryDao;

    @Transactional
    @Override
    public BaseResponse create(String name) {
        PmsProductAttributeCategory productAttributeCategory = new PmsProductAttributeCategory();
        productAttributeCategory.setName(name);
        int count= productAttributeCategoryMapper.insertSelective(productAttributeCategory);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse delete(Long id) {
        return BaseResponse.setResultSuccess(productAttributeCategoryMapper.deleteByPrimaryKey(id));
    }

    @Override
    public BaseResponse update(Long id, String name) {
        PmsProductAttributeCategory productAttributeCategory = new PmsProductAttributeCategory();
        productAttributeCategory.setName(name);
        productAttributeCategory.setId(id);
        int count= productAttributeCategoryMapper.updateByPrimaryKeySelective(productAttributeCategory);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse<CommonPage<PmsProductAttributeCategoryOut>> getList(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<PmsProductAttributeCategory> productAttributeCategoryList= productAttributeCategoryMapper.selectByExample(new PmsProductAttributeCategoryExample());
        return BaseResponse.setResultSuccess(CommonPage.restPage(MallBeanUtils.copyDoToDto(productAttributeCategoryList,PmsProductAttributeCategoryOut.class)));
    }

    @Override
    public BaseResponse<PmsProductAttributeCategoryOut> getItem(Long id) {
        return BaseResponse.setResultSuccess(MallBeanUtils.dtoToDo(productAttributeCategoryMapper.selectByPrimaryKey(id),PmsProductAttributeCategoryOut.class));
    }

    @Override
    public BaseResponse<List<PmsProductAttributeCategoryOut>> getListWithAttr() {
        return BaseResponse.setResultSuccess(MallBeanUtils.copyDoToDto(productAttributeCategoryDao.getListWithAttr(),PmsProductAttributeCategoryOut.class));
    }


}
